/**
 * <p>Title: ServiceZMCM23.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 贾雪民
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-13 下午06:30:43  贾雪民  Create
 *</pre>
 */

package com.baosight.sczx.zm.cm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCM;
import com.baosight.sczx.zm.cm.domain.ZMCMMaterialBasicInfo;
import com.baosight.sczx.zm.cm.domain.ZMCMMaterialType;
import com.baosight.sczx.zm.cm.domain.ZMCMTeammaterialplanDetail;
import com.baosight.sczx.zm.cs.domain.ZMCSEngineering;

/**区队需求计划明细
 * <p>Title: ServiceZMCM23.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 贾雪民
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-13 下午06:30:43  贾雪民  Create
 *          2011-9-09 下午04:30:43  张坤  Modify
 *          
 *</pre>
 */

public class ServiceZMCM23 extends ServiceEPBase{

	/**初始数据
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM23</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-14 上午09:30:43  贾雪民
	 * </p>
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		EiInfo outInfo = this.query(inInfo);	
		return outInfo;
	}

	/**列表查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM23</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-14 上午09:30:43  贾雪民
	 * </p>
	 *
	*/	
	@Override
	public EiInfo query(EiInfo inInfo) {
		inInfo.addBlock("Planresult").set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		String fdPlanId = inInfo.getString("fdPlanId");
		StaticTools.setQueryParameters(inInfo, "fdPlanId", fdPlanId, true);//添加查询条件
		EiInfo outInfo = super.query(inInfo, "ZMCMTeammaterialplanDetail.query",new ZMCMTeammaterialplanDetail(), false, null, EiConstant.queryBlock, "Planresult", "Planresult");
		return outInfo;
	}
	
	/**列表新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM23</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-14 上午09:30:43  贾雪民
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		
		Map<String, String> _map = new HashMap<String, String>();
		//		校验重复
		boolean isRepeat = validate(inInfo, "ZMCMTeammaterialplanDetail.validate", _map);
		
		if(isRepeat) {
			return inInfo;
		}
		else {
			String fdPlanId = inInfo.addBlock("result").getCellStr(0, "fdPlanId");
			String planstatus=inInfo.getBlock("result").getCellStr(0, "fdPlanStatus"); //当前计划状态
			String fdProjectname = inInfo.getBlock("result").getCellStr(0, "fdProjectname"); //工程名字
			String fdProjectId = inInfo.getBlock("result").getCellStr(0, "fdProjectId"); //工程ID
			String fdApplicationNum = inInfo.getBlock("result").getCellStr(0, "fdApplicationNum"); //申请数量
			
			inInfo.getBlock("result").setCell(0, "fdPlanId",fdPlanId);
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("ZMCMTeammaterialplanDetail"));//主键
			inInfo.getBlock("result").setCell(0, "fdProjectname",fdProjectname);
			inInfo.getBlock("result").setCell(0, "fdProjectId",fdProjectId);
			inInfo.getBlock("result").setCell(0, "fdPurchaseNum",fdApplicationNum);//审核数量
			inInfo.getBlock("result").setCell(0, "fdAuditNum",fdApplicationNum);//审批数量
			EiInfo outInfo = super.insert(inInfo, "ZMCMTeammaterialplanDetail.insert");
			outInfo = super.update(inInfo,"ZMCMTeammaterialplan.updatezhubiao");
			if(planstatus.equals("已上报"))
			{
				if("1".equals(SystemSettingCM.getInstance().getIsApprover()))
				{
					outInfo = super.update(inInfo,"ZMCMTeammaterialplan.update_approving");
				}
				else
				{
					outInfo = super.update(inInfo,"ZMCMTeammaterialplan.update_auditing");
				}
			}		
			return outInfo;
		}
	}

	/**修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM23</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-14 上午09:30:43  贾雪民
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) 
	{
		Map<String, String> _map = new HashMap<String, String>();
		boolean isRepeat = validate(inInfo, "ZMCMTeammaterialplanDetail.validate", _map);
		
		if(isRepeat) 
		{
			return inInfo;
		}
		
		String planstatus=inInfo.getBlock("result").getCellStr(0, "fdPlanStatus"); //当前计划状态
		EiInfo outInfo = super.update(inInfo, "ZMCMTeammaterialplanDetail.update");
		outInfo = super.update(inInfo,"ZMCMTeammaterialplan.updatezhubiao");
		if(planstatus.equals("已上报"))
		{
			if("1".equals(SystemSettingCM.getInstance().getIsApprover()))
			{
				outInfo = super.update(inInfo,"ZMCMTeammaterialplan.update_approving");
			}
			else
			{
				outInfo = super.update(inInfo,"ZMCMTeammaterialplan.update_auditing");
			}
		}
		return outInfo;
	}	

	/**删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM23</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-14 上午09:30:43  贾雪民
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		String planstatus=inInfo.getBlock("Planresult").get("fdPlanStatus").toString(); //当前计划状态
		EiInfo _temp =super.delete(inInfo,"ZMCMTeammaterialplanDetail.delete","Planresult");	
		EiInfo outInfo = super.update(inInfo,"ZMCMTeammaterialplan.updatezhubiao","Planresult");//汇总主表计划总价
		if(planstatus.equals("已上报"))
		{
			if("1".equals(SystemSettingCM.getInstance().getIsApprover()))
			{
				outInfo = super.update(inInfo,"ZMCMTeammaterialplan.update_approving","Planresult");
			}
			else
			{
				outInfo = super.update(inInfo,"ZMCMTeammaterialplan.update_auditing","Planresult");
			}			
		}
		outInfo = this.query(inInfo);
		outInfo.setMsg(_temp.getMsg());
		outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM23</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-14 上午09:30:43  贾雪民
	 * </p>
	 *
	 */
	public EiInfo delete1(EiInfo inInfo) {
				
		EiInfo _temp =super.delete(inInfo,"ZMCMTeammaterialplanDetail.delete","Planresult");
		EiInfo outInfo = super.update(inInfo,"ZMCMTeammaterialplan.update_fdPlanStatus","Planresult");
		outInfo = super.update(inInfo,"ZMCMTeammaterialplan.updatezhubiao","Planresult");//汇总主表计划总价
//	    outInfo = super.update(inInfo,"ZMCMTeammaterialplan.update_fdPlanStatus");
		outInfo = this.query(inInfo);
		outInfo.setMsg(_temp.getMsg());
		outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
		
	/**
	 * 明细customview 取物资信息
	 * <p>Function: queryforCustomview</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-21 上午09:30:43  贾雪民
	 * </p>
	 *
	 */
	public EiInfo materialcode(EiInfo inInfo) {
		EiInfo outInfo = super.query(inInfo, "ZMCMMaterialBasicInfo.queryForMaterialcode",new ZMCMMaterialBasicInfo());
		return outInfo;
	}

	/**
	 * 校验物资编号是否重复
	 * <p>Function: validateNo</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-26 下午08:13:00 张坤
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		String fdMaterialType = inInfo.getBlock("result").getCellStr(0, "fdMaterialType");
		String fdMaterialId = inInfo.getBlock("result").getCellStr(0, "fdMaterialId");
		String fdFinanceClassify = inInfo.getBlock("result").getCellStr(0, "fdFinanceClassify");
		String fdSpecModel = inInfo.getBlock("result").getCellStr(0, "fdSpecModel");
		String fdMaterialName = inInfo.getBlock("result").getCellStr(0, "fdMaterialName");
		String fdKeyId = inInfo.getBlock("result").getCellStr(0, "fdKeyId");
		String fdPlanId = inInfo.getBlock("result").getCellStr(0, "fdPlanId");
		
		if(fdMaterialType.equals("有编码物资"))
		{
			params.put("fdMaterialId", fdMaterialId);
			params.put("fdPlanId", fdPlanId);
			params.put("fdFinanceClassify", fdFinanceClassify);
		}
		else
		{
			params.put("fdMaterialName", fdMaterialName);
			params.put("fdSpecModel", fdSpecModel);
			params.put("fdPlanId", fdPlanId);
			params.put("fdFinanceClassify", fdFinanceClassify);
		}
		
		//编辑时校验需要剔除本记录
		if(!(fdKeyId==null || fdKeyId.equals(""))){
			params.put("fdKeyId", fdKeyId);
		}

		List result = dao.query(sqlName, params);
		
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("当前需求计划下已有该类物资！");
			return true;
		}
		return false;
	}
	
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询工程</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-11下午01:10:52 张坤
	 * </p>
	 *
	*/
	public EiInfo queryEngineering(EiInfo inInfo) {
		String fdParentId = inInfo.getString("fdParentId");
		String fdParentTable = inInfo.getString("fdParentTable");
		
		StaticTools.setQueryParameters(inInfo, "fdParentId", fdParentId, true);//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdParentTable", fdParentTable, true);//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdNotStatus", 2, true);//添加查询条件
		
		EiInfo outInfo = super.query(inInfo, "ZMCSEngineering.query", new ZMCSEngineering());
		return outInfo;
	}
	
	/**物资分类查询
	 * <p>Function: type</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMH0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-7下午05:35:57 贾雪民
	 * </p>
	 *
	 */
	public EiInfo type(EiInfo info) 
	{
		info = super.query(info, "ZMCMMaterialType.queryallType", new ZMCMMaterialType());
		return info;
	}
	
	/**
	 * <p>Function: querySerialNumber</p>
	 * <p>Description: 查询顺序号</p>
	 * <p>History:
	 *    2012-6-4下午01:10:52 bobswang
	 * </p>
	 *
	*/
	public EiInfo querySerialNumber(EiInfo inInfo) {
		String fdPlanId = inInfo.getString("fdPlanId");
		
		Map inMap = new HashMap();
		inMap.put("fdPlanId", fdPlanId);//添加查询条件
		
		List _list = dao.query("ZMCMTeammaterialplanDetail.querySerialNum", inMap);
		
		EiInfo outInfo = new EiInfo();
		if(_list.size() > 0)
		{
			outInfo.set("fdSerialNum", (Integer.parseInt(((Map)_list.get(0)).get("fdSerialNum").toString())+1)+"");
		}
		else
		{
			outInfo.set("fdSerialNum", "1");
		}
		return outInfo;
	}
}